home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_068 / mg1b / main.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  290 lines

  1. /*
  2.  *        Mainline, macro commands.
  3.  */
  4. #include    "def.h"
  5.  
  6. int    thisflag;            /* Flags, this command        */
  7. int    lastflag;            /* Flags, last command        */
  8. int    curgoal;            /* Goal column            */
  9. BUFFER    *curbp;                /* Current buffer        */
  10. WINDOW    *curwp;                /* Current window        */
  11. BUFFER    *bheadp;            /* BUFFER listhead        */
  12. WINDOW    *wheadp = (WINDOW *)NULL;    /* WINDOW listhead        */
  13. KEY    kbdm[NKBDM] = {(KCTLX|')')};    /* Macro            */
  14. KEY    *kbdmip;            /* Input  for above        */
  15. KEY    *kbdmop;            /* Output for above        */
  16. char    pat[NPAT];            /* Pattern            */
  17. #ifdef    HASH
  18. SYMBOL    *symbol[NSHASH];        /* Symbol table listhead.    */
  19. #else
  20. /* should really be a *symbol, but don't want to break the hash code yet */
  21. SYMBOL  *symbol[1] = {(SYMBOL *)NULL};
  22. #endif
  23. SYMBOL    *binding[NKEYS];        /* Key bindings.        */
  24. #ifdef    DPROMPT
  25. extern char prompt[], *promptp;        /* delayed prompting        */
  26. #endif
  27. VOID    edinit();
  28.  
  29. VOID
  30. main(argc, argv) char *argv[]; {
  31.     register KEY    c;
  32.     register int    f;
  33.     register int    n;
  34.     register int    mflag;
  35. #ifdef    STARTUP
  36.     char        *sfile, *startupfile();
  37. #endif
  38.     char        bname[NBUFN];
  39.  
  40. #ifdef SYSINIT
  41.     SYSINIT;                /* system dependent.    */
  42. #endif
  43.     vtinit();                /* Virtual terminal.    */
  44.     edinit();                /* Buffers, windows.    */
  45.     keymapinit();                /* Symbols, bindings.    */
  46.     /* doing update() before reading files causes the error messages from
  47.      * the file I/O show up on the screen.  (and also an extra display
  48.      * of the mode line if there are files specified on the command line.)
  49.      */
  50.     update();
  51. #ifdef    STARTUP                    /* User startup file.    */
  52.     if ((sfile = startupfile()) != NULL)
  53.         (VOID) load(sfile);
  54. #endif
  55.     while (--argc > 0) {
  56.         adjustcase(*++argv);
  57.                 makename(bname, *argv);
  58.         curbp = bfind(bname, TRUE);
  59.         (VOID) showbuffer(curbp, curwp, 0);
  60.         (VOID) readin(*argv);
  61.     }
  62.     lastflag = 0;                /* Fake last flags.    */
  63. loop:
  64. #ifdef    DPROMPT
  65.     *(promptp = prompt) = '\0';
  66.     if(epresf == KPROMPT) eerase();
  67. #endif
  68.     update();                /* Fix up the screen.    */
  69.     c = getkey(KPROMPT);
  70.     if (epresf == TRUE) {
  71.         eerase();
  72.         update();
  73.     }
  74.     f = FALSE;
  75.     n = 1;
  76.     if (((KMETA|'0') <= c && c <= (KMETA|'9')) || c == (KMETA|'-')) {
  77.         f = TRUE;
  78.         c = c & ~KMETA;
  79.     } else if (c == (KCTRL|'U')) {
  80.         f = TRUE;
  81.         n = 4;
  82.         while ((c=getkey(KNOMAC | KPROMPT)) == (KCTRL|'U')) 
  83.             n *= 4;
  84.     }
  85.     if (f == TRUE) {
  86.         if ((c>='0' && c<='9') || c=='-') {
  87.             if (c == '-') {
  88.                 n = 0;
  89.                 mflag = TRUE;
  90.             } else {
  91.                 n = ((int) c) - '0';
  92.                 mflag = FALSE;
  93.             }
  94.             while ((c=getkey(KNOMAC | KPROMPT))>='0' && c<='9')
  95.                 n = 10*n + ((int) c) - '0';
  96.             if (mflag != FALSE)
  97.                 n = -n;
  98.         }
  99.     }
  100.     if (kbdmip != NULL) {            /* Terminate macros.    */
  101.         if (c!=(KCTLX|')') && kbdmip>&kbdm[NKBDM-6]) {
  102.             (VOID) ctrlg(FALSE, 0, KRANDOM);
  103.             goto loop;
  104.         }
  105.         if (f != FALSE) {
  106.             kbdmip[-1] = (KEY) (KCTRL|'U');/* overwrite ESC */
  107.             *kbdmip++ = (KEY) n;
  108.             *kbdmip++ = (KEY) c;
  109.         }
  110.     }
  111.     switch (execute(c, f, n)) {        /* Do it.        */
  112.         case TRUE: break;
  113.         case ABORT:
  114.                ewprintf("Quit");    /* and fall through */
  115.         case FALSE: default:
  116.                ttbeep();
  117.                if (kbdmip != NULL) {
  118.                    kbdm[0] = (KEY) (KCTLX|')');
  119.                    kbdmip = NULL;
  120.                }
  121.     }
  122.     goto loop;
  123. }
  124.  
  125. /*
  126.  * Command execution. Look up the binding in the the
  127.  * binding array, and do what it says. Return a very bad status
  128.  * if there is no binding, or if the symbol has a type that
  129.  * is not usable (there is no way to get this into a symbol table
  130.  * entry now). Also fiddle with the flags.
  131.  */
  132. execute(c, f, n) KEY c; {
  133.     register SYMBOL    *sp;
  134.     register int    status;
  135.  
  136.     if ((sp=binding[c]) != NULL) {
  137.         thisflag = 0;
  138.         status = (*sp->s_funcp)(f, n, c);
  139.         lastflag = thisflag;
  140.         return (status);
  141.     }
  142.     lastflag = 0;
  143.     return (FALSE);
  144. }
  145.  
  146. /*
  147.  * Initialize all of the buffers
  148.  * and windows. The buffer name is passed down as
  149.  * an argument, because the main routine may have been
  150.  * told to read in a file by default, and we want the
  151.  * buffer name to be right.
  152.  */
  153. VOID
  154. edinit() {
  155.     register BUFFER    *bp;
  156.     register WINDOW    *wp;
  157.  
  158.     bheadp = NULL;
  159.     bp = bfind("*scratch*", TRUE);        /* Text buffer.        */
  160.     wp = (WINDOW *)malloc(sizeof(WINDOW));    /* Initial window.    */
  161.     if (bp==NULL || wp==NULL) panic("edinit");
  162.     curbp  = bp;                /* Current ones.    */
  163.     wheadp = wp;
  164.     curwp  = wp;
  165.     wp->w_wndp  = NULL;            /* Initialize window.    */
  166.     wp->w_bufp  = bp;
  167.     bp->b_nwnd  = 1;            /* Displayed.        */
  168.     wp->w_linep = bp->b_linep;
  169.     wp->w_dotp  = bp->b_linep;
  170.     wp->w_doto  = 0;
  171.     wp->w_markp = NULL;
  172.     wp->w_marko = 0;
  173.     wp->w_toprow = 0;
  174.     wp->w_ntrows = nrow-2;            /* 2 = mode, echo.    */
  175.     wp->w_force = 0;
  176.     wp->w_flag  = WFMODE|WFHARD;        /* Full.        */
  177. }
  178.  
  179. /*
  180.  * Quit command. If an argument, always
  181.  * quit. Otherwise confirm if a buffer has been
  182.  * changed and not written out. Normally bound
  183.  * to "C-X C-C".
  184.  */
  185. /*ARGSUSED*/
  186. quit(f, n, k) {
  187.     register int    s;
  188.  
  189.     if ((s = anycb(FALSE)) == ABORT) return ABORT;
  190.     if (s == FALSE
  191.     || eyesno("Some modified buffers exist, really exit") == TRUE) {
  192.         vttidy();
  193.         exit(GOOD);
  194.     }
  195.     return TRUE;
  196. }
  197.  
  198. /*
  199.  * Begin a keyboard macro.
  200.  * Error if not at the top level
  201.  * in keyboard processing. Set up
  202.  * variables and return.
  203.  */
  204. /*ARGSUSED*/
  205. ctlxlp(f, n, k) {
  206.     if (kbdmip!=NULL || kbdmop!=NULL) {
  207.         ewprintf("Already defining kbd macro!");
  208.         return (FALSE);
  209.     }
  210.     ewprintf("Defining kbd macro...");
  211.     kbdmip = &kbdm[0];
  212.     return (TRUE);
  213. }
  214.  
  215. /*
  216.  * End keyboard macro. Check for
  217.  * the same limit conditions as the
  218.  * above routine. Set up the variables
  219.  * and return to the caller.
  220.  */
  221. /*ARGSUSED*/
  222. ctlxrp(f, n, k) {
  223.     if (kbdmip == NULL) {
  224.         ewprintf("Not defining kbd macro.");
  225.         return (FALSE);
  226.     }
  227.     ewprintf("Keyboard macro defined");
  228.     kbdmip = NULL;
  229.     return (TRUE);
  230. }
  231.  
  232. /*
  233.  * Execute a macro.
  234.  * The command argument is the
  235.  * number of times to loop. Quit as
  236.  * soon as a command gets an error.
  237.  * Return TRUE if all ok, else
  238.  * FALSE.
  239.  */
  240. /*ARGSUSED*/
  241. ctlxe(f, n, k) {
  242.     register KEY    c;
  243.     register int    af;
  244.     register int    an;
  245.     register int    s;
  246.  
  247.     if (kbdmip!=NULL || kbdmop!=NULL) {
  248.         ewprintf("Not now");
  249.         return (FALSE);
  250.     }
  251.     if (n < 0) 
  252.         return (TRUE);
  253.     do {
  254.         kbdmop = &kbdm[0];
  255.         do {
  256.             af = FALSE;
  257.             an = 1;
  258.             if ((c = *kbdmop++) == (KCTRL|'U')) {
  259.                 af = TRUE;
  260.                 an = (int) *kbdmop++;
  261.                 c  = *kbdmop++;
  262.             }
  263.             s = TRUE;
  264.         } while (c!=(KCTLX|')') && (s=execute(c, af, an))==TRUE);
  265.         kbdmop = NULL;
  266.     } while (s==TRUE && --n);
  267.     return (s);
  268. }
  269.  
  270. /*
  271.  * User abort. Should be called by any input routine that sees a C-g
  272.  * to abort whatever C-g is aborting these days. Currently does
  273.  * nothing.
  274.  */
  275. /*ARGSUSED*/
  276. ctrlg(f, n, k) {
  277.     return (ABORT);
  278. }
  279.  
  280. /*
  281.  * Display the version. All this does
  282.  * is copy the version string onto the echo line.
  283.  */
  284. /*ARGSUSED*/
  285. showversion(f, n, k) {
  286.  
  287.     ewprintf(version);
  288.     return TRUE ;
  289. }
  290.